package com.css.zfzx.sjcj.modules.qhseismicfortification.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.qhseismicfortification.repository.entity.QhSeismicFortificationEntity;
import com.css.zfzx.sjcj.modules.qhseismicfortification.service.QhSeismicFortificationService;
import com.css.zfzx.sjcj.modules.qhseismicfortification.viewobjects.QhSeismicFortificationQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author tao
 * @date 2020-11-17
 */
@Slf4j
@RestController
@RequestMapping("/qh/qhSeismicFortifications")
public class QhSeismicFortificationController {
    @Autowired
    private QhSeismicFortificationService qhSeismicFortificationService;

    @GetMapping("/queryQhSeismicFortifications")
    public RestResponse queryQhSeismicFortifications(HttpServletRequest request, QhSeismicFortificationQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = qhSeismicFortificationService.queryQhSeismicFortifications(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getQhSeismicFortification(@PathVariable String id) {
        RestResponse response = null;
        try{
            QhSeismicFortificationEntity qhSeismicFortification = qhSeismicFortificationService.getQhSeismicFortification(id);
            response = RestResponse.succeed(qhSeismicFortification);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping
    public RestResponse saveQhSeismicFortification(@RequestBody QhSeismicFortificationEntity qhSeismicFortification) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhSeismicFortificationService.saveQhSeismicFortification(qhSeismicFortification);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateQhSeismicFortification(@RequestBody QhSeismicFortificationEntity qhSeismicFortification)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhSeismicFortificationService.updateQhSeismicFortification(qhSeismicFortification);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @DeleteMapping
    public RestResponse deleteQhSeismicFortifications(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhSeismicFortificationService.deleteQhSeismicFortifications(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(qhSeismicFortificationService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导入
     */
    @PostMapping("/importFile")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            String s = qhSeismicFortificationService.importExcel(file);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导出
     */
    @GetMapping("/exportFile/{ids}")
    public RestResponse exportExcel(@PathVariable String ids, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = qhSeismicFortificationService.exportExcel(ids, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导出失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 提交
     */
    @PostMapping("/submit/{ids}")
    public RestResponse submitQhDrillOverview(@PathVariable String ids)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhSeismicFortificationService.submitQhSeismicFortification(ids);
            json.put("message", "提交成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "提交失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 质检通过
     * @param qhSeismicFortification
     * @return
     */
    @PutMapping("/inspectionPassed")
    public RestResponse inspectionPassed(@RequestBody QhSeismicFortificationEntity qhSeismicFortification)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhSeismicFortificationService.inspectionPassed(qhSeismicFortification);
            json.put("message", "质检通过修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "质检通过修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 质检未通过
     * @param qhSeismicFortification
     * @return
     */
    @PutMapping("/inspectionFailed")
    public RestResponse inspectionFailed(@RequestBody QhSeismicFortificationEntity qhSeismicFortification)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhSeismicFortificationService.inspectionFailed(qhSeismicFortification);
            json.put("message", "质检未通过修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "质检未通过修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}